﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Acsys.Entity;
using System.Collections;
using Microsoft.Reporting.WinForms;

namespace Acsys
{
    public partial class CrTransactionReportForm : Form, IChildForm
    {
        Currency m_ccy = new Currency();
        IMainForm m_mainForm;
       // ReportDocument m_crReportDocument = new ReportDocument();
        ReportDataSource m_rdlcReportDocument = new ReportDataSource();
        string appPath = Application.StartupPath;

        public CrTransactionReportForm(Form parent, string name)
        {
            InitializeComponent();
            dateTimePickerDateFrom.Value = DateTime.Today.Subtract(new TimeSpan(90, 0, 0, 0, 0));
            InitializeButtonClick();
            this.Name = name;
            this.MdiParent = parent;
            m_mainForm = (IMainForm)parent;
            loadDebitType();
        }
        private void loadDebitType()
        {
            ArrayList drTypelist = (ArrayList)RepositoryFactory.GetInstance().CodeNameRepository().GetAll(new CreditType());
            drTypelist.Add(new CreditType(0, "ALL"));
            comboBoxDebitType.DataSource = drTypelist;
            comboBoxDebitType.SelectedIndex = drTypelist.Count - 1;
            ArrayList currencylist = (ArrayList)RepositoryFactory.GetInstance().CodeNameRepository().GetAll(new Currency());
            currencylist.Add(new Currency(0, "ALL"));
            comboBoxCurrency.DataSource = currencylist;
            comboBoxCurrency.SelectedIndex = currencylist.Count - 1;
            ArrayList vessellist = (ArrayList)RepositoryFactory.GetInstance().CodeNameRepository().GetAll(new Vessel());
            vessellist.Add(new Vessel(0, "NONE"));
            vessellist.Add(new Vessel(0, "ALL"));
            comboBoxVessel.DataSource = vessellist;
            comboBoxVessel.SelectedIndex = vessellist.Count - 1;

            ArrayList pymentstatus = new ArrayList();
            pymentstatus.Add("ALL");
            pymentstatus.AddRange(Enum.GetNames(typeof(PaymentStatus)));
            comboBoxPaymentStatus.DataSource = pymentstatus;
            comboBoxPaymentStatus.SelectedIndex = 0;
        }
        private void InitializeButtonClick()
        {
            toolStripButtonRunReport.Click += new EventHandler(RunReport);
        }

        void RunReport(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = RepositoryFactory.GetInstance().CrTrReportRepository().GetListReport(
                    dateTimePickerDateFrom.Value, dateTimePickerDateTo.Value,
                    ((CreditType)comboBoxDebitType.SelectedItem).ID == 0,
                     ((CreditType)comboBoxDebitType.SelectedItem).ID,
                      ((Currency)comboBoxCurrency.SelectedItem).ID == 0,
                     ((Currency)comboBoxCurrency.SelectedItem).ID,
                      ((Vessel)comboBoxVessel.SelectedItem).CODE == "ALL",
                     ((Vessel)comboBoxVessel.SelectedItem).ID, 
                     ((Vessel)comboBoxVessel.SelectedItem).CODE == "NONE",
                     comboBoxPaymentStatus.SelectedItem.ToString(),
                     comboBoxPaymentStatus.SelectedItem.ToString() == "ALL"
                     );
                ds.WriteXml("report.xml", XmlWriteMode.WriteSchema);
                //m_crReportDocument.Load("CrTransactionReport.rpt"); 
                //m_crReportDocument.SetDataSource(ds);
                //crystalReportViewer.ReportSource = m_crReportDocument;
                //crystalReportViewer.RefreshReport();

                reportViewer1.LocalReport.ReportPath = appPath + @"\CrTransactionReport.rdlc";
                reportViewer1.LocalReport.DataSources.Add(m_rdlcReportDocument);
                m_rdlcReportDocument.Name = "temp_dataset_table_temp";
                m_rdlcReportDocument.Value = new BindingSource(ds, "Report"); ;
                reportViewer1.RefreshReport();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }
        private void setEditMode(EditMode editmode)
        {
            toolStripButtonRunReport.Enabled = (editmode == EditMode.New || editmode == EditMode.Update) && m_mainForm.CurrentUser.FORM_CRTRRPT_ACCESS;
            ReloadMainFormButton();
        }
        private void ReloadMainFormButton()
        {
            m_mainForm.EnableButtonSave(false);
            m_mainForm.EnableButtonClear(false);
            m_mainForm.EnableButtonDelete(false);
            m_mainForm.EnableButtonEdit(false);
            m_mainForm.EnableButtonRefresh(true);
        }

        private void DrTransactionReportFormActivated(object sender, EventArgs e)
        {
            ReloadMainFormButton();
        }

        private void DrTransactionReportForm_Load(object sender, EventArgs e)
        {

        }

        #region IChildForm Members

        public void Save(object sender, EventArgs e)
        {
        }

        public void Edit(object sender, EventArgs e)
        {
        }

        public void Delete(object sender, EventArgs e)
        {
        }

        public void Clear(object sender, EventArgs e)
        {
        }

        #endregion

        public void Refresh(object sender, EventArgs e)
        {
            loadDebitType();
        }
    }
}
